function isMobile() {
    // 判断是否为移动设备
    return (
        typeof window.orientation !== "undefined" || // 判断是否存在window.orientation属性，此属性在移动设备上一般存在
        navigator.userAgent.indexOf('IEMobile') !== -1 || // 判断是否为Windows Phone
        navigator.userAgent.indexOf('iPhone') !== -1 || // 判断是否为iPhone
        navigator.userAgent.indexOf('Android') !== -1 && navigator.userAgent.indexOf('Mobile') !== -1 || // 判断是否为Android手机
        navigator.userAgent.indexOf('BlackBerry') !== -1 || // 判断是否为BlackBerry
        navigator.userAgent.indexOf('Opera Mini') !== -1 // 判断是否为Opera Mini浏览器
    );
}
layui.use(["jquery"], function () {
    var $ = layui.$;
    var id = getQueryString("id");
    if(isMobile()){
        $(".print-button").hide();
    }
    request($, "get", requestUrl + "/service/focus-service/test/publicApi/test?id=" + id, null, function (res) {
        if (res.check_type == 1) {
            location.replace("checkReport.html?id=" + id);
            return;
        }
        var qrcode= new QRCode(document.getElementById("qrcode"), {
            text: "http://focus.xinshengcorp.com/device/report.html?id="+res.pid,
            width: 150,
            height: 150,
            colorDark: "#000000",
            colorLight: "#ffffff",

        });
        request($, "get", requestUrl + "/service/focus-service/courseSetting/publicApi/courseByLevel?age=" + res.age + "&level=" + res.averageScore, null, function (res) {
            $("#root").height("auto");
            var names = "";
            var courseCount = 0;
            for (var i = 0; i < res.length; i++) {
                names += "<span style='font-weight:bold;color:#FA9F49'>" + res[i].type + "</span>:";
                for (var j = 0; j < res[i].items.length; j++) {
                    courseCount += res[i].items[j].hour;
                }
                //names+="<p><span style='font-weight:bold'>"+res[i].items[j].phase+":</span> ";
                names += "<p>"
                names += res[i].items[0].courseNames.slice(0, 5).join("，");
                names += " 等...</p>";
                //}                        
            }
            $("#courseName").html(names);
            $("#courseHour").text(courseCount + "个课时以上")
        });
        $("#name").text(res.name);
        $("#sex").text(res.sex);
        $("#age").text(res.age);
        $("#orgName").text(res.orgName);
        $("#teacher").text(res.teacher);
        $("#testDate").text(res.testDate);
        profile_initHearingChart("profile_HearingChart", res.data);
        profile_initVisionChart("profile_visionChart", res.data);
        typeChartInit("hearingChart", res.data, "听觉");
        typeChartInit("visionChart", res.data, "视觉");
        for (var i = 0; i < res.data.length; i++) {
            var data = res.data[i];
            //gaugeChartInit("item" + i, res.data, res.data[i].item_no);
            $("#item" + i).text(data.levelScore);
            $(".itemLevelName" + i).html(getItemName(data.levelScore, data.level))
            $("#visionName" + i).text(data.itemName);
            $("#visionScore" + i).text(data.levelScore + " 级");
            $("#hearingName" + i).text(data.itemName);
            $("#hearingScore" + i).text(data.levelScore + " 级");
            $(".hearingStar" + i).html(createStar(data.levelScore));
            $("#levelName" + i).text(data.level);
            $(".itemName" + i).text(data.itemName);
            $("#itemScore" + i).text(data.levelScore + " 级");
        }
    });
    $(function () {
        if ($("#root").width() < 570) {
            $(".title_bg").css("background", "none");
        }
        // else {
        //     setTimeout(() => {
        //         htmlToPdf("评测报告", "#root",false);
        //     }, 3000);
        // }
    })
})
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
function request($, type, url, data, success) {
    $.ajax({
        url: url,
        data: data,
        type: type,
        success: function (res) {
            success(res)
        }
    })
}
function getItemName(score, level) {
    var color = "117901";
    if (level == "极弱") color = "E23F34";
    else if (level == "较弱") color = "EC5E5A";
    else if (level == "不足") color = "FA9F49";
    else if (level == "中等") color = "50A9B2";
    return "<span style='color:#" + color + "'>" + level + "</span>";
}
function createStar(num) {
    var html = "";
    for (var i = 0; i < num; i++) {
        html += "<span class='activ_star'>★</span>"
    }
    for (var i = 0; i < 5 - num; i++) {
        html += "<span class='noActiv_star'>★</span>"
    }
    return html;
}
// function htmlToPdf(title, id, page = true) {
//     html2canvas(
//         document.querySelector(id),
//         {
//             allowTaint: false,
//             taintTest: false,
//             logging: false,
//             useCORS: true,
//             dpi: window.devicePixelRatio * 4, //将分辨率提高到特定的DPI 提高四倍
//             scale: 4, //按比例增加分辨率
//         }
//     ).then((canvas) => {
//         if (!page) {
//             var contentWidth = canvas.width / 4
//             var contentHeight = canvas.height / 4
//             var jsPDF=jspdf.jsPDF
//             var pdf = new jsPDF('', 'pt', [contentWidth, contentHeight])
//             var pageData = canvas.toDataURL('image/jpeg', 1.0)
//             pdf.addImage(pageData, 'JPEG', 0, 0, contentWidth, contentHeight)
//             pdf.save(title + '.pdf');
//         } else {
//             var jsPDF=jspdf.jsPDF
//             var pdf = new jsPDF('p', 'mm', 'a4'); //A4纸，纵向
//             var ctx = canvas.getContext('2d'),
//                 a4w = 190,
//                 a4h = 272, //A4大小，210mm x 297mm，四边各保留10mm的边距，显示区域190x277
//                 imgHeight = Math.floor((a4h * canvas.width) / a4w); //按A4显示比例换算一页图像的像素高度
//                 var renderedHeight = 0;
//             while (renderedHeight < canvas.height) {
//                 var page = document.createElement('canvas');
//                 page.width = canvas.width;
//                 page.height = Math.min(imgHeight, canvas.height - renderedHeight); //可能内容不足一页
//                 //用getImageData剪裁指定区域，并画到前面创建的canvas对象中
//                 page
//                     .getContext('2d')
//                     .putImageData(
//                         ctx.getImageData(
//                             0,
//                             renderedHeight,
//                             canvas.width,
//                             Math.min(imgHeight, canvas.height - renderedHeight),
//                         ),
//                         0,
//                         0,
//                     );
//                 pdf.addImage(
//                     page.toDataURL('image/jpeg', 1.0),
//                     'JPEG',
//                     10,
//                     10,
//                     a4w,
//                     Math.min(a4h, (a4w * page.height) / page.width),
//                 ); //添加图像到页面，保留10mm边距
//                 renderedHeight += imgHeight;
//                 if (renderedHeight < canvas.height) {
//                     pdf.addPage(); //如果后面还有内容，添加一个空页
//                 }
//                 // delete page;
//             }
//             pdf.save(title + '.pdf');
//         }
//     });
// }